/*
 *  CalcPsiDelta.c
 *  AutoEllips
 *
 *  Created by Simon Martin on 04/05/2010.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#include "CalcPsiDelta.h"
int VoltageToPsiDelta(double* Voltage,int size,  double* psi, double* delta,WINDOW *mainwnd)
//int VoltageToPsiDelta(double* Voltage,int size, double angle, double* psi, double* delta,WINDOW *mainwnd)
{
// Now have the data, need to convert to Psi and Delta

double k0=0.;
double k1=0.;
double k2=0.;

// k0= is the average value of the readings.
int i;

for (i=0;i<size;i++) k0+=Voltage[i];
k0/=size;

wprintw(mainwnd,"k0=%.4f\n",k0);
refresh();
double rangle;

rangle=5.0*M_PI/180.; 
// k1 =2*sum(Vi*cos(2*anglei))/N
// k1 =2*sum(Vi*sin(2*anglei))/N

for (i=0;i<size;i++) k1+=(Voltage[i]*cos(2.0*rangle*i));
for (i=0;i<size;i++) k2+=(Voltage[i]*sin(2.0*rangle*i)); 

k1*=(2.0/size);
k2*=(2.0/size);

wprintw(mainwnd,"k0=%2.3f k1=%2.3f k2=%2.3f \n",k0,k1,k2);
refresh();
*psi=atan(sqrt((k0+k1)/(k0-k1)));
*delta=acos(k2/sqrt((k0*k0-k1*k1)));
	return 0;
}

